package com.lvmama.rhino.web;

import com.github.pagehelper.PageInfo;
import com.lvmama.entity.system.SysUser;
import com.lvmama.rhino.base.BaseControllerSupport;
import com.lvmama.rhino.entity.PushConfigurationVO;
import com.lvmama.rhino.enums.CommonType;
import com.lvmama.rhino.pojo.PushConfiguration;
import com.lvmama.rhino.service.PushConfigurationService;
import com.lvmama.rhino.web.sso.SystemSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author majun
 * @Description:推送配置管理后台
 * @create: 2017-04-11 17:39
 */
@Controller
@RequestMapping("pushConfiguration")
public class PushConfigurationController extends BaseControllerSupport {

    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private PushConfigurationService pushConfigurationService;

    /**
     * 推送配置管理首页面查询
     *
     * @param model
     * @param pushConfigurationVO
     * @return
     */
    @RequestMapping("pushConfigurationManagement")
    public String pushConfigurationIndex(ModelMap model, PushConfigurationVO pushConfigurationVO) {
        //枚举状态
        CommonType[] getFlagType = CommonType.getDelFlagType();
        List<CommonType> flagType = Arrays.asList(getFlagType);

        PageInfo<PushConfiguration> page = pushConfigurationService.queryPushConfiguration(pushConfigurationVO);
        List<PushConfiguration> list = page.getList();
        model.addAttribute("PushConfigurationVO", pushConfigurationVO);
        model.addAttribute("page", page);
        model.addAttribute("pushConfigurationList", list);
        model.addAttribute("flagTypeList", flagType);

        return "application/pushStatistic/pushConfigurationManagement";
    }


    /**
     * 跳转到推送配置添加页面
     *
     * @param model
     * @param pushConfiguration
     * @return
     */
    @RequestMapping("add")
    public String pushConfigurationAdd(ModelMap model, PushConfiguration pushConfiguration, PushConfigurationVO pushConfigurationVO) {

        //枚举状态
        CommonType[] getFlagType = CommonType.getDelFlagType();
        List<CommonType> flagType = Arrays.asList(getFlagType);

        model.addAttribute("flagTypeList", flagType);

        //参数名称加上唯一性约束，在添加的时候要检查是否已存在相同的参数名称
        List<PushConfiguration> pushConfigurationList = pushConfigurationService.queryParameterName();
        List<String> StrList = new ArrayList<String>();
        for (PushConfiguration obj : pushConfigurationList) {
            String str = obj.getParameterName();
            StrList.add(str);
        }
        model.addAttribute("strList", StrList);


        return "application/pushStatistic/pushConfigurationAdd";
    }

    /**
     * 保存推送配置(添加和修改的)
     *
     * @param model
     * @param pushConfiguration
     * @param pushConfigurationVO
     * @return
     */
    @RequestMapping("save")
    public String SavepushConfigurationAdd(ModelMap model, PushConfiguration pushConfiguration, PushConfigurationVO pushConfigurationVO) {

        //获取当前系统操作人
        SysUser user = SystemSession.getUser();
        String people = user.getCharacterName();
        if (StringUtils.isBlank(pushConfiguration.getId())) {
            pushConfiguration.setCreatePeople(people);
        } else {
            pushConfiguration.setUpdatePeople(people);
        }

        pushConfigurationService.addPushConfiguration(pushConfiguration);

        //用于返回页面列表展示
        CommonType[] getFlagType = CommonType.getDelFlagType();
        List<CommonType> flagType = Arrays.asList(getFlagType);
        pushConfigurationVO.setParameterName(null);
        pushConfigurationVO.setState(null);
        PageInfo<PushConfiguration> page = pushConfigurationService.queryPushConfiguration(pushConfigurationVO);
        List<PushConfiguration> list = page.getList();
        model.addAttribute("page", page);
        model.addAttribute("pushConfigurationList", list);
        model.addAttribute("flagTypeList", flagType);

        return "application/pushStatistic/pushConfigurationManagement";
    }

    /**
     * 跳转到编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("edit")
    public String pushConfigurationEdit(ModelMap model, String id) {
        //枚举状态
        CommonType[] getFlagType = CommonType.getDelFlagType();
        List<CommonType> flagType = Arrays.asList(getFlagType);
        PushConfiguration pushConfigurationNew = pushConfigurationService.queryPushCongfigurationById(id);
        model.addAttribute("pushConfigurationNew", pushConfigurationNew);
        model.addAttribute("flagTypeList", flagType);

        return "application/pushStatistic/pushConfigurationAdd";
    }

    /**
     * 删除推送配置---暂时不用
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteConfiguration")
    @ResponseBody
    public Map<String, Object> delete(ModelMap model, PushConfigurationVO pushConfigurationVO, String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        pushConfigurationService.deleteConfigurationById(id);
        resultMap.put("code", 1);

        return resultMap;
    }

}
